package com.dhgate.spu.analysis.index;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhgate.spu.analysis.bean.Attr;
import com.dhgate.spu.analysis.bean.Product;
import com.dhgate.spu.analysis.collector.ICollector;
import com.dhgate.spu.analysis.consts.CommonConsts;
import com.dhgate.spu.analysis.consts.SpuStandardConsts;
import com.dhgate.spu.analysis.consts.SysConsts;
import com.dhgate.spu.analysis.dao.DAOFactory;
import com.dhgate.spu.analysis.util.StringUtil;

/**
 * @author: wangguodong
 * @create: 2014-2-13 下午4:38:55
 * @description:
 */
public class IndexProduct {
	private static final Logger logger = LoggerFactory.getLogger(IndexProduct.class);
	private static int INDEX_NUMBER = 1;

	public static void process(String prodLineId) throws Exception {
		logger.info("delete index by query: prodlineid:"+prodLineId+" begin");
		SysConsts.SOLR_SERVER.deleteByQuery("prodlineid:"+prodLineId);
		SysConsts.SOLR_SERVER.commit();
		logger.info("delete index by query: prodlineid:"+prodLineId+" end");
		long startTime = System.currentTimeMillis();
		int tableNum = SysConsts.MYSQL_TABLE_KEY_LIST.size();
		int tableCount = 1;
		String attrValIds = SpuStandardConsts.getAttrValIds(prodLineId);
		if(StringUtil.isEmpty(attrValIds)){
			logger.error("attrValIds is null prodlineId:"+prodLineId);
			throw new Exception("attrValIds is null prodlineId:"+prodLineId);
		}
		for (String l2sid : SysConsts.MYSQL_TABLE_KEY_LIST) {	
			
			List<Product> pList = DAOFactory.getDAOInstance().getProducts(l2sid, prodLineId, attrValIds);
			for (Product p : pList) {
				process(p);
				p.setIndexNumber(INDEX_NUMBER);
				INDEX_NUMBER++;
			}
			SolrClient.index(pList);
			logger.info("process "+tableCount + "/" + tableNum + " table:" + l2sid + " have add "+INDEX_NUMBER+" docs,cost time:"+(System.currentTimeMillis()-startTime)/1000+"s");
			tableCount++;
		}

		// 遍历mysql数据库和表
		// 查询product
		// 提取型号
		// 组织doc 
		// 将doc批量提交到solr
	}
	
	
	private static void process(Product p){
		ICollector collector = SpuStandardConsts.getCollector(p.getProdLineId());
		if(collector == null){
			p.setExtField(CommonConsts.ExtFieldName.ERROR, CommonConsts.ErrorValue.NOT_HAVE_COLLECTOR);
		}else{
			collector.process(p);
		}
		
	}
	
	
	public static void main(String[] args) throws Exception {
		Product p = new Product();
		p.setProductName(" I9500 S4 5 MX Inch Triple SIM Card Smartphone Android 4.2 MTK6572 Dual Core 3G GPS 512MB RAM 4GB H9503 Android Cell Phone");
		List<Attr> attrs = new ArrayList<Attr>();
//		p.setAttrs(attrs);
		Attr attr = new Attr();
		attrs.add(attr);
		List<Long> attrValIdList = new ArrayList<Long>();
		attrValIdList.add(1188281L);
		attr.setAttrValIds(attrValIdList);
		
		attr = new Attr();
		attrs.add(attr);
//		attr.setAttrId(AttrConsts.PHONE_RAM_ATTR_ID);
		attrValIdList = new ArrayList<Long>();
		attrValIdList.add(897L);
		attr.setAttrValIds(attrValIdList);
		
		
//		ProductUtil.fillBrandAndRamId(p);
//		System.out.println(p.getModel());
		
		
	}

}
